******************************************************************************************
*Main dataset in paper: Merge primary votes data with primary money data
******************************************************************************************
use "/Users/daniellethomsen/Desktop/votedata_primary.dta", clear
merge 1:1 year state district rep special using "/Users/daniellethomsen/Desktop/moneydata_primary.dta"
drop _merge 
merge 1:1 year state district rep special using "/Users/daniellethomsen/Desktop/earlymoneydata_primary.dta"
drop _merge 

gen diff_votereceipts = competitiveprivotes_575-competitiveprireceipts_575
gen diff_toptwovotereceipts = competitiveprivotes_toptwo20-competitiveprireceipts_toptwo20
gen diff_effective = npricands - effectivecands
gen diff_effectivevotes = effectivecands_votes - effectivecands
gen diff_herfindex = (herfindex_votes - herfindex_receipts)/herfindex_votes

gen diff_votedisb = competitiveprivotes_575-competitivepridisb_575
gen diff_toptwovotedisb = competitiveprivotes_toptwo20-competitivepridisb_toptwo20
gen diff_effectivedisb = npricands - effectivecands_disb
gen diff_effectivevotesdisb = effectivecands_votes - effectivecands_disb

gen diff_PRVYEvotereceipts = competitiveprivotes_575-PRVYEcompetitivereceipts_575
gen diff_PRVYEtoptwovotereceipts = competitiveprivotes_toptwo20-PRVYEreceipts_toptwo20
gen diff_PRVYEeffective = npricands - PRVYEeffectivecands
gen diff_PRVYEeffectivevotes = effectivecands_votes - PRVYEeffectivecands

lab var diff_votereceipts "Difference (Votes — Receipts)"
lab var diff_toptwovotereceipts "Difference, 20 Pt Margin (Votes — Receipts)"
lab var diff_effective "Difference (Total — Effective)"
lab var diff_effectivevotes "Difference (Eff Votes — Eff Receipts)"
lab var diff_herfindex "Change in HHI (Votes-Receipts)/Votes"

lab var diff_votedisb "Difference (Votes — Spending)"
lab var diff_toptwovotedisb "Difference, 20 Pt Margin (Votes — Spending)"
lab var diff_effectivedisb "Difference (Total — Effective Spending)"
lab var diff_effectivevotesdisb "Difference (Eff Votes — Eff Spending)"

lab var diff_PRVYEvotereceipts "Difference (Votes — Receipts), At Previous Year End"
lab var diff_PRVYEtoptwovotereceipts "Difference, 20 Pt Margin (Votes — Receipts), At Previous Year End"
lab var diff_PRVYEeffective "Difference (Total — Effective), At Previous Year End"
lab var diff_PRVYEeffectivevotes "Difference (Eff Votes — Eff Receipts), At Previous Year End"

save "/Users/daniellethomsen/Desktop/votemoneydata_primary.dta", replace

******************************************************************************************
*Additional dataset: Merge primary votes data with primary money data (fundraising totals exclude loans)
******************************************************************************************
use "/Users/daniellethomsen/Desktop/votedata_primary.dta", clear
merge 1:1 year state district rep special using "/Users/daniellethomsen/Desktop/moneydata_primary_excludeloans.dta"
drop _merge

gen diff_votereceipts = competitiveprivotes_575-competitiveprireceipts_575
gen diff_toptwovotereceipts = competitiveprivotes_toptwo20-competitiveprireceipts_toptwo20
gen diff_effective = npricands - effectivecands
gen diff_effectivevotes = effectivecands_votes - effectivecands
gen diff_herfindex = (herfindex_votes - herfindex_receipts)/herfindex_votes

lab var diff_votereceipts "Difference (Votes — Receipts)"
lab var diff_toptwovotereceipts "Difference, 20 Pt Margin (Votes — Receipts)"
lab var diff_effective "Difference (Total — Effective)"
lab var diff_effectivevotes "Difference (Eff Votes — Eff Receipts)"
lab var diff_herfindex "Change in HHI (Votes-Receipts)/Votes"

save "/Users/daniellethomsen/Desktop/votemoneydata_primary_excludeloans.dta", replace

******************************************************************************************
*Additional dataset: Merge primary votes data with primary money data (fundraising totals exclude dropout candidates)
******************************************************************************************
use "/Users/daniellethomsen/Desktop/votedata_primary.dta", clear
merge 1:1 year state district rep special using "/Users/daniellethomsen/Desktop/moneydata_primary_nodropouts.dta"
drop _merge

gen diff_votereceipts = competitiveprivotes_575-competitiveprireceipts_575
gen diff_toptwovotereceipts = competitiveprivotes_toptwo20-competitiveprireceipts_toptwo20
gen diff_effective = npricands - effectivecands
gen diff_effectivevotes = effectivecands_votes - effectivecands
gen diff_herfindex = (herfindex_votes - herfindex_receipts)/herfindex_votes

lab var diff_votereceipts "Difference (Votes — Receipts)"
lab var diff_toptwovotereceipts "Difference, 20 Pt Margin (Votes — Receipts)"
lab var diff_effective "Difference (Total — Effective)"
lab var diff_effectivevotes "Difference (Eff Votes — Eff Receipts)"
lab var diff_herfindex "Change in HHI (Votes-Receipts)/Votes"

save "/Users/daniellethomsen/Desktop/votemoneydata_primary_nodropouts.dta", replace

******************************************************************************************
*Additional dataset: Merge primary votes data with primary money data (full sample: sample includes disadvantaged-party incumbent-contested primaries and advantaged-party challenger-party primaries; these primary types were excluded in the main analyses following Hirano and Snyder)
******************************************************************************************
use "/Users/daniellethomsen/Desktop/votedata_primary_fullsample.dta", clear
merge 1:1 year state district rep special using "/Users/daniellethomsen/Desktop/moneydata_primary_fullsample.dta"
drop _merge 

gen diff_votereceipts = competitiveprivotes_575-competitiveprireceipts_575
gen diff_toptwovotereceipts = competitiveprivotes_toptwo20-competitiveprireceipts_toptwo20
gen diff_effective = npricands - effectivecands
gen diff_effectivevotes = effectivecands_votes - effectivecands
gen diff_herfindex = (herfindex_votes - herfindex_receipts)/herfindex_votes

lab var diff_votereceipts "Difference (Votes — Receipts)"
lab var diff_toptwovotereceipts "Difference, 20 Pt Margin (Votes — Receipts)"
lab var diff_effective "Difference (Total — Effective)"
lab var diff_effectivevotes "Difference (Eff Votes — Eff Receipts)"
lab var diff_herfindex "Change in HHI (Votes-Receipts)/Votes"

save "/Users/daniellethomsen/Desktop/votemoneydata_primary_fullsample.dta", replace

******************************************************************************************
*General election dataset: Merge general election votes data with general election money data
******************************************************************************************
use "/Users/daniellethomsen/Desktop/votedata_general.dta", clear
merge 1:1 year state district special using "/Users/daniellethomsen/Desktop/moneydata_general.dta"
drop _merge

gen diff_genvotereceipts = competitivegen_votes-competitivegen_receipts
gen diff_geneffective = ngencands - effectivegencands
gen diff_geneffectivevotes = effectivegencands_votes - effectivegencands

lab var diff_genvotereceipts "Difference in Competitive, General Election (Votes — Receipts)"
lab var diff_geneffective "Difference in Candidates, General Election (Total — Effective)"
lab var diff_geneffectivevotes "Difference in Candidates, General Election (Eff Votes — Eff Receipts)"

save "/Users/daniellethomsen/Desktop/votemoneydata_general.dta", replace

******************************************************************************************
*Combined primary and general election dataset
******************************************************************************************
use "/Users/daniellethomsen/Desktop/votemoneydata_general.dta", clear
replace special = 0 if year==1992 & state=="OH" & district==1

*Generate duplicate observations for merge with votemoneydata_primary.dta 
gen rep = 1
expand 2, gen(newobs)
replace rep=0 if newobs==1 

merge 1:1 year state district rep special using "/Users/daniellethomsen/Desktop/votemoneydata_primary_fullsample.dta"

*Drop primary races with missing values of effective candidates
gen effectivemissing = 1 if effectivecands==.
egen maxeffectivemissing = max(effectivemissing), by(year state district rep special)
drop if maxeffectivemissing==1

*Add effective primary and general candidates
egen overallcands_effective = rowtotal(effectivegencands effectivecands)
replace overallcands_effective = . if overallcands_effective==0
egen overallcands_total = rowtotal(ngencands npricands)

*Add primary and general competitive race
egen overallcomp_575votes = rowtotal(competitivegen_votes competitiveprivotes_575)
egen overallcomp_575receipts = rowtotal(competitivegen_receipts competitiveprireceipts_575)

collapse (mean) overallcands_effective overallcands_total overallcomp_575receipts overallcomp_575votes (max) openseat competitivedistrict, by(year state district special)

duplicates tag year state district, gen(dup)
drop if dup==1

sum overallcomp_575votes overallcomp_575receipts
sum overallcands_effective overallcands_total

collapse (mean) overallcands_effective overallcands_total overallcomp_575receipts overallcomp_575votes, by(year openseat competitivedistrict)

lab var year "Year"
lab var overallcands_effective "Effective Candidates, Primary and General"
lab var overallcands_total "On-Ballot Candidates, Primary and General"
lab var overallcomp_575receipts "Competition with Receipts, Primary and General"
lab var overallcomp_575votes "Competition with Votes, Primary and General"
lab var openseat "Open Seat"
lab var competitivedistrict "Competitive District"

save "/Users/daniellethomsen/Desktop/combinedprimarygeneral.dta", replace

******************************************************************************************
*END
